package com.coderside.fragment.io.commons;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;

public class TestIoDemo {

	private void phasmethod(){
		//拷贝文件
		try {
			File src = new File("test.dat");
	        File dest = new File("test.dat.bak");
	          
	        FileUtils.copyFile(src, dest);
	    } catch(IOException ioe) {
	        System.out.println("Problem copying file.");
	    }
	    
	    //拷贝文件到指定目录
	    try {
	        File src = new File("test.dat");
	        File dir = new File("./temp");
	 
	        FileUtils.copyFileToDirectory(src, dir);
	    } catch(IOException ioe) {
	        System.out.println("Problem copying file to dir.");
	    }
	      
	    //往文件里写一个字符串
	    try {
	    	String string = "Blah blah blah";
	        File dest = new File("test.tmp");
	          
	        FileUtils.writeStringToFile(dest, string, "ISO-8859-1");
	    } catch( IOException ioe ) {
	        System.out.println("Error writing out a String.");       
	    }
	      
	    //拷贝数据
	    try {
	        Writer writer = new FileWriter("test.dat");
	        InputStream inputStream = TestIoDemo.class.getClass().getResourceAsStream("/io/web.xml");
	        IOUtils.copy(inputStream, writer);
	        writer.close();
	        inputStream.close();
	    } catch (IOException e) {
	        System.out.println("Error copying data");
	    }
	 
	    //拷贝数据
	    try {
	        File src = new File("test.txt");
	        OutputStream output = new FileOutputStream(src);
	        InputStream inputStream = TestIoDemo.class.getClass().getResourceAsStream("/io/web.xml");
	        IOUtils.copy(inputStream, output);
//	        writer.close();
	        inputStream.close();
	        output.close();
	    } catch (IOException e) {
	        System.out.println("Error copying data");
	    }
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//直接将IO流转成字符串 
		InputStream in = null;
		try {
			in = new URL( "http://jakarta.apache.org" ).openStream();
			try {   
				System.out.println(IOUtils.toString(in)); 
			} finally {   
				IOUtils.closeQuietly(in); 
			}
			//读取文本文件的所有行
			File file = new File("/commons/io/project.properties"); 
			List lines = FileUtils.readLines(file, "UTF-8");
			//路径处理 
			String filename = "C:/commons/io/../lang/project.xml"; 
			String normalized = FilenameUtils.normalize(filename); 
			// result is "C:/commons/lang/project.xml"
			//获取目录空间 
			long freeSpace = FileSystemUtils.freeSpace("C:/"); 
			//打印文件的所有行
			LineIterator it = FileUtils.lineIterator(file, "UTF-8"); 
			try {   
				while (it.hasNext()) {     
					String line = it.nextLine();     
					/// do something with line   
				} 
			} finally {   
				LineIterator.closeQuietly(it); 
			}
			
			//文件删除
			File file1 = new File(("io/project.properties"));
		    String display = FileUtils.byteCountToDisplaySize(file1.length());
		    System.out.println("project.xml is " + display);
		    FileUtils.forceDelete(file1);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
	}

}